/**
 * Copyright © 2013 南京思创信息技术有限公司. *
 * 版权所有.
 *
 * 类名　　    : OnlineList
 * 功能概要    : 在线列表页面. * 做成日期    : 2013/06/07 wangxw
 */
package com.sevenit.nj.yrmtfish.online;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sevenit.nj.yrmtfish.R;
import com.sevenit.nj.yrmtfish.common.AppManager;
import com.sevenit.nj.yrmtfish.common.FileBase;
import com.sevenit.nj.yrmtfish.common.ImageViewFitW;
import com.sevenit.nj.yrmtfish.common.ServerMaster;
import com.sevenit.nj.yrmtfish.common.StaticPara;
import com.sevenit.nj.yrmtfish.entity.OnlineData;
import com.sevenit.nj.yrmtfish.entity.ServerData;
import com.sevenit.nj.yrmtfish.login.Login;
import com.sevenit.nj.yrmtfish.common.Utils;

/**
 * 在线列表页面的类
 * @author wangxw
 * @version 1.0
 */
@SuppressLint("HandlerLeak") 
public class OnlineList extends Activity {
	
	//当面画面的名称
	private String _strPageName;
	
	//提示对话框.
	ProgressDialog _proDialog;
		
	//请求服务器方法
	private ServerMaster _server;
	
	//列表列表适配器.
	private AdapterOnlineList _adapterList;
	
	/////////////////////记录当前画面状态/////////////////
	//存储信息父ID，因为多层次的信息目录都可能使用本画面显示，所以需要记录每层的父ID.
	private List<String> _lsParentId;
	//当前画面的父ID.
	private String _parentId;
	//当前画面的父ID.
	private String _parentName;
	//查询下页数据时，从什么类型开始查（1:从栏目开始；2：从商品开始）
	private int _searchType;
	//从第几条数据开始查(0为第一条).
	private int _startRow;
		
	/**
	 * onCreate
	 */
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		//自定义标题栏
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		//设置画面布局
		this.setContentView(R.layout.online_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		
		//添加该Activity到AppManager
		AppManager.addActivity(this);
		
		//初始化画面数据.
		initPageData(); 
		
		//获取前画面传过来的参数.
		getPara();
		
		//设置画面控件
		setControl();
				
		//线程查询数据.
		loadPage(_parentId);
	}
	
	/**
	 * 初始化画面数据.
	 */
	private void initPageData() {
		
		//当面画面的名称
		_strPageName = this.getClass().getName();
		
		//初始化全局对象
		_server = new ServerMaster(this, StaticPara.MemConf.AppServerIp, StaticPara.MemConf.AppServerPort);
		
		//创建列表适配器.
		_adapterList = new AdapterOnlineList(OnlineList.this, 
									(ListView)findViewById(R.id.list_online));
		
		//初始化画面状态.
		_lsParentId = new ArrayList<String>(); 
		_searchType = 1;
		_startRow = 0;
	}
	
	/**
	 * 获取前画面传过来的参数.
	 */
	private void getPara() {
		
		String strInfoId = "";
		
		//获取传入的参数.
		Intent intentT = getIntent();
		strInfoId = intentT.getStringExtra("info_id");
		
		if(!Utils.isNull(strInfoId)){
			_parentId = strInfoId;
		}else{
			_parentId = "-2";
		}
	}

	/**
	 * 设置画面控件
	 */
	private void setControl() {
		
		//设置标题文字
		TextView txtTitle = (TextView)findViewById(R.id.txt_title);
		txtTitle.setText(R.string.txt_online_list_title);
		//设置为粗体
		TextPaint paint = txtTitle.getPaint();
		paint.setFakeBoldText(true);
		
		//设置[返回]按钮按下效果
		ImageButton button = (ImageButton)findViewById(R.id.btn_back);
		button.setOnTouchListener(new ImageButton.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					arg0.setBackgroundResource(R.drawable.btn_back_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					arg0.setBackgroundResource(R.drawable.btn_back);
				}
				return false;
			}
        });	
		//点击[返回]按钮的响应.
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(_lsParentId.size()>1){
					loadPage(_lsParentId.get(_lsParentId.size() - 2));
	        	}else{
	        		AppManager.closePage(OnlineList.class.getName());
	        	}
			}
		});
		
		//去除分隔线.
		ListView listView = (ListView)findViewById(R.id.list_online);
		listView.setDivider(null);
		
		//初始化底部共通区域.
		setBtnBottom();
	}
	
	/**
	 * 初始化底部共通区域.
	 */
	private void setBtnBottom() {
		
		ImageViewFitW imgBtn;
		
		//设置[首页]按钮按下效果
		imgBtn = (ImageViewFitW)findViewById(R.id.btn_tail_shouye);
		imgBtn.setOnTouchListener(new ImageViewFitW.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_shouye_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_shouye);
				}
				return false;
			}
        });	
		//点击[首页]按钮的响应.
		imgBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AppManager.goMenu();
				//去除画面跳转时的动画效果
				overridePendingTransition(0, 0);
			}
		});
		
		//设置[车况]按钮按下效果
		imgBtn = (ImageViewFitW)findViewById(R.id.btn_tail_chekuang);
		imgBtn.setOnTouchListener(new ImageViewFitW.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_chekuang_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_chekuang);
				}
				return false;
			}
        });	
		//点击[车况]按钮的响应.
		imgBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(!StaticPara.isLogin()){
					Toast.makeText(OnlineList.this, R.string.msg_err_not_login, Toast.LENGTH_SHORT).show();
				}else{
					//Intent intent = new Intent();
					//intent.setClass(OnlineList.this, CarConditionThis.class);
					//startActivity(intent);
					//去除画面跳转时的动画效果
					overridePendingTransition(0, 0);
					AppManager.closePage(OnlineList.class.getName());
				}
			}
		});

		//设置[VIP]或[登录]按钮.
		setBtnVip();
		
		//设置[设置]按钮按下效果
		imgBtn = (ImageViewFitW)findViewById(R.id.btn_tail_shezhi);
		imgBtn.setOnTouchListener(new ImageViewFitW.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_shezhi_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_shezhi);
				}
				return false;
			}
        });	
		//点击[设置]按钮的响应.
		imgBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Intent intent = new Intent();
				//intent.setClass(OnlineList.this, Setting.class);
				//startActivity(intent);
				//去除画面跳转时的动画效果
				overridePendingTransition(0, 0);
				AppManager.closePage(OnlineList.class.getName());
			}
		});

		//设置[更多]按钮按下效果
		imgBtn = (ImageViewFitW)findViewById(R.id.btn_tail_gengduo);
		imgBtn.setOnTouchListener(new ImageViewFitW.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_gengduo_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					((ImageViewFitW)arg0).setImageResource(R.drawable.btn_gengduo);
				}
				return false;
			}
        });	
		//点击[更多]按钮的响应.
		imgBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(OnlineList.this, R.string.msg_btn_more, Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	/**
	 * 设置[登录]或[VIP]按钮.
	 */
	private void setBtnVip() {
		
		ImageViewFitW imgBtn;
		
		if(!StaticPara.isLogin()){
			//设置[登陆]按钮按下效果
			imgBtn = (ImageViewFitW)findViewById(R.id.btn_tail_denglu);
			imgBtn.setImageResource(R.drawable.btn_denglu);
			imgBtn.setOnTouchListener(new ImageViewFitW.OnTouchListener() {
				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
						((ImageViewFitW)arg0).setImageResource(R.drawable.btn_denglu_p);
					} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
						((ImageViewFitW)arg0).setImageResource(R.drawable.btn_denglu);
					}
					return false;
				}
	        });	
			//点击[登陆]按钮的响应.
			imgBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(OnlineList.this, Login.class);
					startActivity(intent);
					AppManager.closePage(OnlineList.class.getName());
				}
			});
		}else{
			//设置[VIP]按钮按下效果
			imgBtn = (ImageViewFitW)findViewById(R.id.btn_tail_denglu);
			imgBtn.setImageResource(R.drawable.btn_vip);
			imgBtn.setOnTouchListener(new ImageViewFitW.OnTouchListener() {
				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
						((ImageViewFitW)arg0).setImageResource(R.drawable.btn_vip_p);
					} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
						((ImageViewFitW)arg0).setImageResource(R.drawable.btn_vip);
					}
					return false;
				}
	        });	
			//点击[VIP]按钮的响应.
			imgBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					//Intent intent = new Intent();
					//intent.setClass(OnlineList.this, CarVipDetail.class);
					//startActivity(intent);
					//去除画面跳转时的动画效果
					overridePendingTransition(0, 0);
					AppManager.closePage(OnlineList.class.getName());
				}
			});
		}
	}
	
	/**
	 * 监听手机硬件上的返回等按钮
	 */
	@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK &&	event.getRepeatCount() == 0) {
        	if(_lsParentId.size()>1){
        		loadPage(_lsParentId.get(_lsParentId.size() - 2));
        	}else{
        		AppManager.closePage(OnlineList.class.getName());
        	}
            return false; 
        }
        return false; 
    }
	
	/**
	 * 设置查询条件，查询.
	 * @param strParentId	:	父ID.
	 */
	private void loadPage(String strParentId){
		
		//父ID
		_parentId = strParentId;
		
		//如果是装载新画面.
		if(_lsParentId.size() <= 0 || !_parentId.equals(_lsParentId.get(_lsParentId.size() - 1))){
			_searchType = 1;
			_startRow = 0;
		}
		
		//线程查询数据.
		sendMsg(StaticPara.MSG_PAGE_LOADING);
	}
	
	/**
	 * 记录画面状态.
	 */
	private void savePagePara(){
		
		if(_lsParentId.size() <= 0){
			//装载顶层情况下，加入父ID到队列.
			_lsParentId.add(_parentId);
		} else if(_lsParentId.size() > 0 && _parentId.equals(_lsParentId.get(_lsParentId.size() - 1))){
			//本层分页的情况下，父ID不变.
		} else if(_lsParentId.size() > 1 && _parentId.equals(_lsParentId.get(_lsParentId.size() - 2))){
			//后退的情况下，删除一个父ID.
			_lsParentId.remove(_lsParentId.size() - 1);
		}else {
			//前进的情况下，加入父ID到队列.
			_lsParentId.add(_parentId);
		}
	}
	
	/**
	 * 回滚画面状态.
	 */
	private void rollBackPagePara(){
		
		if(_lsParentId.size() <= 0){
			//装载顶层情况下，父ID不变.
		}else{
			_parentId = _lsParentId.get(_lsParentId.size() - 1);
		}
	}
	
	/**
	 * 新线程查询数据.
	 * @author wangxw
	 */
	class LoadThread implements Runnable {

		//private int _iWorkType = 0;
		
		public LoadThread(int iDoType){
			//_iWorkType = iDoType;
		}
		
		/**
		 * 
		 */
		public void run() {
			//查询数据.
			if(!doSearch()){
				rollBackPagePara();
			} else {
				savePagePara();
				
				try{
					//下载图片
					loadImg();
				}catch(Exception e){
					//可能还未下载完成又重新装载了.
				}
			}
		}
		
		/**
		 * 查询数据.
		 */
		public boolean doSearch() {
			Map<String, String> hMap = new HashMap<String, String>();
			hMap.put("CarNumber", StaticPara.MemConf.CarNo);
			hMap.put("ID", _parentId);
			hMap.put("Type", "0");
			hMap.put("SearchType", String.valueOf(_searchType));
			hMap.put("StartRow", String.valueOf(_startRow));
			hMap.put("PageMaxCount", String.valueOf(StaticPara.LIST_MAX_COUNT));
			ServerData sd = _server.doRequest("Get4SList",hMap);

			if (sd.getStatus() == -1) {
				//当前网络不可用.
				sendMsg(StaticPara.MSG_ERR_NET);
				return false;
			} else if (sd.getStatus() == -2) {
				//不能连接服务器，请稍候再试！
				sendMsg(StaticPara.MSG_ERR_SERVER);
				return false;
			} else if (sd.getStatus() == -99){
				//未知的错误.
				sendMsg(StaticPara.MSG_ERR_UNKNOW);
				return false;
			} else if (sd.getStatus() == 1){
				String strTemp;
				int iTemp;
				
				//未查询到数据.
				if(sd.count() <= 1){
					sendMsg(StaticPara.MSG_ERR_NODATA);
					return false;
				}
				
				//返回值是否正常.
				sd.first();
				strTemp = sd.getString("ReturnCode");
				if(!"00".equals(strTemp)){
					//未知的错误.
					sendMsg(StaticPara.MSG_ERR_UNKNOW);
					return false;
				}
				
				//页码 不正确.
				iTemp = 0;
				strTemp = sd.getString("TotalPage");
				if(Utils.isHalfNum(strTemp)){
					iTemp = Integer.parseInt(strTemp);
				}
				if(iTemp <= 0){
					_adapterList.setPageMax(_adapterList.getPageCurrent());
					//刷新画面数据.
					sendMsg(StaticPara.MSG_PAGE_SHOW_DATA);
					return false;
				}
				
				//如果是装载新画面，清空原画面.
				if(_lsParentId.size() <= 0 || !_parentId.equals(_lsParentId.get(_lsParentId.size() - 1))){
					_adapterList.init();
				}
				//当前页码加1
				_adapterList.setPageCurrent(_adapterList.getPageCurrent() + 1);
				//最多显示页数
				_adapterList.setPageMax(iTemp>StaticPara.LIST_MAX_PAGE?StaticPara.LIST_MAX_PAGE:iTemp);
				
				//设置列表数据.
				_parentName = "";
				while(sd.next()){
					OnlineData item = new OnlineData();
					
					//类型（0:栏目、1:商品）
					iTemp = 0;
					strTemp = sd.getString("Type");
					if(Utils.isHalfNum(strTemp)){
						iTemp = Integer.parseInt(strTemp);
						item.setDataType(iTemp);
					}
					if(_searchType == 1 && iTemp == 1){
						_searchType = 2;
						_startRow = 0;
					}
					_startRow++;
					//ID
					strTemp = sd.getString("Id");
					if(!Utils.isNull(strTemp)){
						item.setId(strTemp);
					}
					//名称
					strTemp = sd.getString("Name");
					if(!Utils.isNull(strTemp)){
						item.setName(strTemp);
					}
					//简介
					strTemp = sd.getString("OverView");
					if(!Utils.isNull(strTemp)){
						item.setMemo(strTemp);
					}
					//图片
					item.setImgId(R.drawable.online_pic);//默认图片
					strTemp = sd.getString("PicAdd");
					if(!Utils.isNull(strTemp)){
						List<String> lsImg = new ArrayList<String>();
						lsImg.add(strTemp);
						item.setImgLstPathHttp(lsImg);
					}
					
					//父栏目名称.
					strTemp = sd.getString("ParentName");
					if(!Utils.isNull(strTemp)){
						_parentName = strTemp;
					}				
					
					_adapterList.add(item);
				}
				//先显示画面数据.
				sendMsg(StaticPara.MSG_PAGE_SHOW_DATA);
				
				return true;
			}
			
			return false;
		}

		/**
		 * 下载图片
		 */
		private void loadImg() {
			
			List<OnlineData> lsData = _adapterList.getListData();
			if(lsData == null || lsData.size() <= 0){
				return;
			}
			
			//获取图片
			FileBase fb = new FileBase();
			String strImgHttp;
			for(int i = 0;i < lsData.size();i++){

				strImgHttp = "";
				
				//获取网络图片地址.
				List<String> lsTemp = lsData.get(i).getImgLstPathHttp();
				if(lsTemp != null && lsTemp.size() > 0){
					strImgHttp = lsTemp.get(0);
				}
				
				//如果没有获取到.
				if(Utils.isNull(strImgHttp)){
					continue;
				}
				//获取网络图片.
				strImgHttp = StaticPara.getFileServer() + strImgHttp;
				Drawable dr = fb.getImgFromUrl(strImgHttp, true);
				if(dr == null){
					continue;
				}
				List<Drawable> lsT = lsData.get(i).getImageLstObj();
				if(lsT.size() <= 0){
					lsT.add(dr);
				}else{
					lsT.set(0, dr);
				}
				//刷新画面数据.
				sendMsg(StaticPara.MSG_PAGE_SHOW_DATA);
			}
		}
	}

	/**
	 * 向界面发送消息.
	 * UI显示动作不能在线程里做.
	 */
	private void sendMsg(int Msg){
		Message message = new Message();
		Bundle bundle = new Bundle();
		bundle.putInt(_strPageName, Msg);
		message.setData(bundle);
		myHandler.sendMessage(message);
	}
		
	//主线程消息处理.
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			int iMsg = msg.getData().getInt(_strPageName);
			switch (iMsg) {
			
			case StaticPara.MSG_ERR_NET:
				if (_proDialog != null) {
					_proDialog.dismiss();
				}
				Toast.makeText(OnlineList.this, R.string.msg_net_error, Toast.LENGTH_LONG).show();
				break;
			case StaticPara.MSG_ERR_SERVER:
				if (_proDialog != null) {
					_proDialog.dismiss();
				}
				Toast.makeText(OnlineList.this, R.string.msg_server_error, Toast.LENGTH_LONG).show();
				break;
			case StaticPara.MSG_ERR_UNKNOW:
				if (_proDialog != null) {
					_proDialog.dismiss();
				}
				Toast.makeText(OnlineList.this, R.string.msg_unknown_error, Toast.LENGTH_LONG).show();
				break;
			case StaticPara.MSG_PAGE_LOADING:
				// 显示进度
				_proDialog = ProgressDialog.show(OnlineList.this, "提示", "正在加载数据，请稍候...", true, true);
				(new Thread(new LoadThread(StaticPara.MSG_PAGE_LOADING))).start();
				break;
			case StaticPara.MSG_PAGE_REFRESH:
				// 显示进度
				_proDialog = ProgressDialog.show(OnlineList.this, "提示", "正在加载数据，请稍候...", true, true);
				(new Thread(new LoadThread(StaticPara.MSG_PAGE_REFRESH))).start();
				break;
			case StaticPara.MSG_ERR_NODATA:
				if (_proDialog != null) {
					_proDialog.dismiss();
				}
				Toast.makeText(OnlineList.this, R.string.msg_no_data, Toast.LENGTH_LONG).show();
				break;
			case StaticPara.MSG_PAGE_SHOW_DATA:
				if (_proDialog != null) {
					_proDialog.dismiss();
				}
				//画面标题.
				TextView txtTitle = (TextView)findViewById(R.id.txt_title);
				txtTitle.setText(_parentName);
				//装载数据
				ListView listView = (ListView)findViewById(R.id.list_online);
				if(_adapterList.getPageCurrent() <= 1){
					listView.setAdapter(_adapterList);
				}else{
					_adapterList.notifyDataSetChanged();
				}
				break;
			default:
				if (_proDialog != null) {
					_proDialog.dismiss();
				}
			}
			
			super.handleMessage(msg);
		}
	};
	
	/**
	 * 
	 * 保存列表每项的控件
	 * @author wangxw
	 * 
	 */
	public class ViewHolder {
		//ID
		public String strItemId;
		//图片底板.
		public LinearLayout layImg;
		//图片.
		public ImageView imgItem;
		//名称.
		public TextView txtItemName;
		//简介.
		public TextView txtItemMemo;
	}
	
	/**
	 * 列表适配器. 
	 * @author wangxw
	 */
	public class AdapterOnlineList extends BaseAdapter {

		//调用画面.
		private LayoutInflater mInflater;
		//列表体.
		//private ListView mainList;
		
		//数据体.
		private List<OnlineData> listData;
		//当前页码.
		private int iPageCurrent;
		//一共多少页页数.
		private int iPageMax;
		
		/**
		 * 构造函数.
		 * @param context
		 * @param list
		 * @param iItemLayoutId
		 * @param iItemImageId
		 * @param iItemNameId
		 * @param iItemMemoId
		 */
		public AdapterOnlineList(Context context, ListView list) {
			this.mInflater = LayoutInflater.from(context);
			//this.mainList = list;
			this.listData = new ArrayList<OnlineData>();
			this.iPageCurrent = 0;
			this.iPageMax = 0;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}
		
		@Override
		public int getCount() {
			
			int iCount = this.listData.size();
			//底部项目显示"更多..."
			if(this.iPageCurrent < this.iPageMax){
				iCount++;
			}
			
	    	return iCount;
		}

		/**
		 * 自动循环getCount次
		 * 注意：传入参数中position和convertView不是一一对应关系！！
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			// 保存各对象的类
			ViewHolder holder = null;
			
			// 第一条数据.
			if(convertView == null){
				holder = new ViewHolder();
				// 设置convertView的布局文件
				convertView = mInflater.inflate(R.layout.online_list_item, null);
				holder.layImg = (LinearLayout) convertView.findViewById(R.id.lay_info_img);
				holder.imgItem = (ImageView) convertView.findViewById(R.id.img_Info);
				holder.txtItemName = (TextView) convertView.findViewById(R.id.txt_name);
				holder.txtItemMemo = (TextView) convertView.findViewById(R.id.txt_memo);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			//整个标题栏点击事件
			//这里要响应整个标题栏点击，
			//所以需要将其他控件设置成不获取焦点，也不可以点击
			holder.imgItem.setFocusable(false);
			holder.imgItem.setClickable(false);
			holder.txtItemName.setFocusable(false);
			holder.txtItemName.setClickable(false);
			holder.txtItemMemo.setFocusable(false);
			holder.txtItemMemo.setClickable(false);
			
			//判断是否是“更多”按钮
			boolean bMoreBtn = false;
			if(position > 0 && position >= this.listData.size()){
				bMoreBtn = true;
			}
			
			//名称栏属性.
			LinearLayout.LayoutParams mclp = (LinearLayout.LayoutParams)holder.txtItemName.getLayoutParams();
			
			//更多按钮
			if(bMoreBtn){
				holder.layImg.setVisibility(View.GONE);
				holder.imgItem.setVisibility(View.GONE);
				holder.txtItemMemo.setVisibility(View.GONE);
				mclp.gravity = Gravity.CENTER;
				holder.txtItemName.setLayoutParams(mclp);
				holder.txtItemName.setText("更多...");
				//设置[更多]按钮按下效果
				convertView.setOnTouchListener(new View.OnTouchListener() {
					@Override
					public boolean onTouch(View arg0, MotionEvent arg1) {
						if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
							arg0.setBackgroundColor(getResources().getColor(R.color.blue_light));
						} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
							arg0.setBackgroundColor(getResources().getColor(R.color.white));
						} else if (arg1.getAction() == MotionEvent.ACTION_CANCEL){
							arg0.setBackgroundColor(getResources().getColor(R.color.white));
						}
						return false;
					}
		        });
				//点击[更多]按钮的响应.
				convertView.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						//线程查询列表数据
						if(_lsParentId.size() > 0){
							TextView txtT = (TextView) v.findViewById(R.id.txt_name);
							txtT.setText("正在查询...");
			        		loadPage(_lsParentId.get(_lsParentId.size() - 1));
			        	}
					}
				});
			} else {
				// 显示数据
				holder.layImg.setVisibility(View.VISIBLE);
				holder.imgItem.setVisibility(View.VISIBLE);
				holder.txtItemName.setVisibility(View.VISIBLE);
				holder.txtItemMemo.setVisibility(View.VISIBLE);
				mclp.gravity = Gravity.LEFT;
				holder.txtItemName.setLayoutParams(mclp);
				holder.strItemId = listData.get(position).getId();
				holder.txtItemName.setText(listData.get(position).getName());
				holder.txtItemMemo.setText(listData.get(position).getMemo());
				
				boolean bShowImgOk = false;
				//图片对象.
				if(listData.get(position).getImageLstObj().size() > 0){
					Drawable di = (Drawable)listData.get(position).getImageLstObj().get(0);
					if(di != null){
			    		holder.imgItem.setImageDrawable(di);
			    		bShowImgOk = true;
			    	}
				}
		    	
		    	//资源图片.
		    	if(!bShowImgOk && listData.get(position).getImgId() > 0){
		    		holder.imgItem.setImageResource(listData.get(position).getImgId());
		    	}
		    	
				//设置每行按下效果
				convertView.setOnTouchListener(new View.OnTouchListener() {
					@Override
					public boolean onTouch(View arg0, MotionEvent arg1) {
						if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
							arg0.setBackgroundColor(getResources().getColor(R.color.blue_light));
						} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
							arg0.setBackgroundColor(getResources().getColor(R.color.white));
						} else if (arg1.getAction() == MotionEvent.ACTION_CANCEL){
							arg0.setBackgroundColor(getResources().getColor(R.color.white));
						}
						return false;
					}
		        });
				//点击每行的响应.
				convertView.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						ViewHolder h = (ViewHolder) v.getTag();
						//线程查询列表数据
						OnlineData od = getOnlineData(h.strItemId);
						if(od!= null && od.getDataType() == 0){
							loadPage(h.strItemId);
						} else if(od!= null && od.getDataType() == 1){
							//商品.
							Intent intent = new Intent();
							intent.setClass(OnlineList.this, OnlineDetail.class);
							intent.putExtra("info_id", od.getId());
							intent.putExtra("info_name", od.getName());
							startActivity(intent);
						} 
					}
				});
			}
			
			return convertView;
		}
		
		//////////////////////////以下为自定义的方法/////////////////////////
		
		/**
		 * 增加一条数据
		 * @param mapItem
		 */
		public void add(OnlineData item){
			listData.add(item);
		}
		
		/**
		 * 设置当前页码
		 * @param iPageCur
		 */
		public void setPageCurrent(int iPageCur){
			this.iPageCurrent = iPageCur;
		}
		
		/**
		 * 获取当前页码
		 */
		public int getPageCurrent(){
			return this.iPageCurrent;
		}
		
		/**
		 * 设置最多显示页数
		 * @param iPageMax
		 */
		public void setPageMax(int iPageMax){
			this.iPageMax = iPageMax;
		}
		
		/**
		 * 获取数据体.
		 */
		public List<OnlineData> getListData(){
			 return listData;
		}
		
		/**
		 * 清除数据 ，初始化.
		 */
		public void init(){
			this.listData.clear();
			this.iPageCurrent = 0;
			this.iPageMax = 0;
		}
		
		/**
		 * 
		 * @param strId
		 * @return
		 */
		public OnlineData getOnlineData(String strId){
			OnlineData od = null;
			boolean bFind = false;
			for(int i=0;i<listData.size();i++){
				od = listData.get(i);
				if(strId.equals(od.getId())){
					bFind = true;
					break;
				}
			}
			
			if(bFind){
				return od;
			}else{
				return null;
			}
		}
	}
}
